package com.smedi.ismedi.cronjob.core.infrastructure.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.cronjob.core.domain.aggregatemodel.JobInfo;
import com.smedi.ismedi.cronjob.core.domain.repository.JobInfoRepository;
import com.smedi.ismedi.cronjob.core.infrastructure.persistence.model.JobInfoEntity;
import com.smedi.ismedi.cronjob.core.infrastructure.persistence.mybatis.mapper.JobInfoMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author u
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CronjobRepositoryImpl extends ServiceImpl<JobInfoMapper, JobInfoEntity> implements JobInfoRepository, IService<JobInfoEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public Boolean doDelete(Long id) {
        JobInfoEntity jobInfoEntity = getById(id);
        jobInfoEntity.setDeleted(Boolean.TRUE);
        updateById(jobInfoEntity);
        return true;
    }

    @Override
    public Boolean doDelete(List<Long> ids) {
        List<JobInfoEntity> jobInfoEntities = listByIds(ids);
        for (JobInfoEntity jobInfoEntity : jobInfoEntities) {
            jobInfoEntity.setDeleted(Boolean.TRUE);
        }
        updateBatchById(jobInfoEntities);
        return true;
    }

    @Override
    public JobInfo doInsert(JobInfo jobInfo) {
        JobInfoEntity jobInfoEntity = dozerUtil.convertor(jobInfo, JobInfoEntity.class);
        save(jobInfoEntity);
        return dozerUtil.convertor(jobInfoEntity, JobInfo.class);
    }

    @Override
    public List<JobInfo> doInsert(List<JobInfo> jobInfos) {
        List<JobInfoEntity> jobInfoEntitys = dozerUtil.convertor(jobInfos, JobInfoEntity.class);
        saveBatch(jobInfoEntitys);
        return dozerUtil.convertor(jobInfoEntitys, JobInfo.class);
    }

    @Override
    public JobInfo doUpdate(JobInfo cronjob) {
        JobInfoEntity jobInfoEntity = dozerUtil.convertor(cronjob, JobInfoEntity.class);
        saveOrUpdate(jobInfoEntity);
        return dozerUtil.convertor(jobInfoEntity, JobInfo.class);
    }

    @Override
    public List<JobInfo> doUpdate(List<JobInfo> jobInfos) {
        List<JobInfoEntity> jobInfoEntities = dozerUtil.convertor(jobInfos, JobInfoEntity.class);
        saveOrUpdateBatch(jobInfoEntities);
        return dozerUtil.convertor(jobInfoEntities, JobInfo.class);
    }

    @Override
    public JobInfo doGet(Long id) {
        JobInfoEntity cronjobEntity = getById(id);
        return dozerUtil.convertor(cronjobEntity, JobInfo.class);
    }

    @Override
    public List<JobInfo> doList() {
        List<JobInfoEntity> list = list();
        return dozerUtil.convertor(list, JobInfo.class);
    }

    @Override
    public List<JobInfo> doList(List<Long> ids) {
        return dozerUtil.convertor(listByIds(ids), JobInfo.class);
    }

    @Override
    public List<JobInfo> doList(JobInfo jobInfo) {
        JobInfoEntity jobInfoEntity = dozerUtil.convertor(jobInfo, JobInfoEntity.class);
        QueryWrapper<JobInfoEntity> queryWrapper = new QueryWrapper<>(jobInfoEntity);
        return dozerUtil.convertor(list(queryWrapper), JobInfo.class);
    }
}
